package com.frogs.ucenter.v1.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.frogs.framework.base.web.BaseController3;
import com.frogs.system.web.api.response.FailResponse;
import com.frogs.system.web.api.response.Response;
import com.frogs.system.web.exception.AppBaseException;
import com.frogs.ucenter.v1.request.ActionLogRequest;
import com.frogs.ucenter.v1.request.PointTaskInfoListRequest;
import com.frogs.ucenter.v1.request.TaskCompleteRequest;
import com.frogs.ucenter.v1.service.TaskService;
import com.frogs.ucenter.v1.util.ApiConstants;

/**
 * 任务相关接口
 *
 * @author lijian
 * @version 2014年10月17日 - 下午5:10:42
 */
@Controller(ApiConstants.VERSION+"taskController")
@RequestMapping("/"+ApiConstants.VERSION)
public class TaskController extends BaseController3{
	
	/**常规*/
	private static final String POINT_TASK_LIST = "/point/task/list";
	private static final String POINT_OTHER_TASK_LIST = "/point/otherTask/list";
	private static final String ATTENTION_POINT_TASK_LIST = "/attention/point/task/list";

	/**主线、常规完成*/
	private static final String TASK_COMPLETE = "/task/complete";
	
	/**动作*/
	private static final String ACTIONLOG_ADD = "/actionLog/add";
	
	@Resource(name=ApiConstants.VERSION + "taskService")
	private TaskService taskService;
	
	/**
	 * 获取用户所有常规任务列表接口
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(POINT_TASK_LIST)
	public Response pointTaskList(HttpServletRequest request) {
		Response response;
		try {
			PointTaskInfoListRequest pointTaskInfoListRequest = new PointTaskInfoListRequest();
			pointTaskInfoListRequest.parse(request);
			response = taskService.getUserPointTask(pointTaskInfoListRequest);
		} catch (Exception e) {
			response = new FailResponse();
			if (e instanceof AppBaseException) {
				response.setTip(e.getMessage());
			} else {
				response.setTip("errro message" + e.getMessage());
			}
		}
		return response;
	}
	
	/**
	 * 获取用户正在进行和待领取的任务
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(POINT_OTHER_TASK_LIST)
	public Response pointOtherTaskList(HttpServletRequest request) {
		Response response;
		try {
			PointTaskInfoListRequest pointTaskInfoListRequest = new PointTaskInfoListRequest();
			pointTaskInfoListRequest.parse(request);
			response = taskService.getUserPointTaskByOther(pointTaskInfoListRequest);
		} catch (Exception e) {
			response = new FailResponse();
			if (e instanceof AppBaseException) {
				response.setTip(e.getMessage());
			} else {
				response.setTip("errro message" + e.getMessage());
			}
		}
		return response;
	}
	
	/**
	 * 获取家人正在进行和待领取的任务列表
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(ATTENTION_POINT_TASK_LIST)
	public Response attentionPointTaskList(HttpServletRequest request) {
		Response response;
		try {
			PointTaskInfoListRequest pointTaskInfoListRequest = new PointTaskInfoListRequest();
			pointTaskInfoListRequest.parse(request);
			response = taskService.getAttentionPointTask(pointTaskInfoListRequest);
		} catch (Exception e) {
			response = new FailResponse();
			if (e instanceof AppBaseException) {
				response.setTip(e.getMessage());
			} else {
				response.setTip("errro message" + e.getMessage());
			}
		}
		return response;
	}
	
	/**
	 * 完成任务接口
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(TASK_COMPLETE)
	public Response taskComplete(HttpServletRequest request) {
		Response response;
		try {
			TaskCompleteRequest completeRequest = new TaskCompleteRequest();
			completeRequest.parse(request);
			response = taskService.completePointTask(completeRequest);
		} catch (Exception e) {
			response = new FailResponse();
			if (e instanceof AppBaseException) {
				response.setTip(e.getMessage());
			} else {
				response.setTip("errro message" + e.getMessage());
			}
		}
		return response;
	}
	
	/**
	 * 完成动作接口
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(ACTIONLOG_ADD)
	public Response actionLogAdd(HttpServletRequest request) {
		Response response;
		try {
			ActionLogRequest actionLogRequest = new ActionLogRequest();
			actionLogRequest.parse(request);
			response = taskService.completeAction(actionLogRequest);
		} catch (Exception e) {
			response = new FailResponse();
			if (e instanceof AppBaseException) {
				response.setTip(e.getMessage());
			} else {
				response.setTip("errro message" + e.getMessage());
			}
		}
		return response;
	}
}
